package com.cn.wanxi.service.impl;

import com.cn.wanxi.iomodel.IOCompnayModel;
import com.cn.wanxi.model.CompnayModel;
import com.cn.wanxi.service.IHomeService;
import com.cn.wanxi.util.CommonPath;
import com.cn.wanxi.util.IO;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class HomeServiceImpl implements IHomeService {


    @Override
    public CompnayModel getCompanyModelFromIo() {
        /**
         * File类的用法，属性和方法，构造方法
         * 重写和重载
         * 1.需要将数据写入记事本
         *  1)指定文件和文件夹
         *  2）判断文件和文件夹是否存在
         *  3）创建文件和文件夹
         *  4）引入输出流OutputStream
         *  5)write
         *  6)需要将字符串转换位字节数组
         * 2.将数据读从记事本取出来
         *  1）判断是否有当前需要读取的文件
         *  2）引入输入流InputStream
         *  3）引入字符流，因为字节流会出现汉字乱码
         *  4）while 和for
         *  5）StringBuilder   String  ，StringBuffer，StringBuilder几个的区别
         *  6）字符串拼接
         * 3.封装到model
         */
        writeNote();
//        String result = readNote();
        return setModel(IO.readNote(CommonPath.COMPANY_NAME.getValue()));
    }


    /**
     * 写入记事本
     */
    private void writeNote() {
        IOCompnayModel ioCompnayModel = new IOCompnayModel();
        List<String> a = new ArrayList<>();
        a.add("logo=" + ioCompnayModel.getLogo());
        a.add("phone=" + ioCompnayModel.getPhone());
        a.add("ad=" + ioCompnayModel.getAd());
        a.add("net=" + ioCompnayModel.getNet());
        a.add("email=" + ioCompnayModel.getEmail());
        a.add("linePhone=" + ioCompnayModel.getLinePhone());
        a.add("address=" + ioCompnayModel.getAddress());
        a.add("version=" + ioCompnayModel.getVersion());
        a.add("introduce=" + ioCompnayModel.getIntroduce());
        IO.tempWrite(a);
    }


    private CompnayModel setModel(String resutl) {

        String[] results = resutl.split(CommonPath.AA.getValue());
        for (int i = 0; i < results.length; i++) {
            System.out.println(i + "---" + results[i]);
        }
        CompnayModel compnayModel = new CompnayModel();
        compnayModel.setLogo(del(results[0]));
        compnayModel.setPhone(del(results[1]));
        compnayModel.setAd(del(results[2]));
        compnayModel.setNet(del(results[3]));
        compnayModel.setEmail(del(results[4]));
        compnayModel.setLinePhone(del(results[5]));
        compnayModel.setAddress(del(results[6]));
        compnayModel.setVersion(del(results[7]));
        compnayModel.setIntroduce(del(results[8]));
        return compnayModel;
    }

    private static String del(String value) {
        return value.split("=")[1];
    }
}

